package ie.ucc.cs.anggao.finalproject.client.ui.mainpanel.infopane.PageableFlextTable;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTMLTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class BaseFlexTable extends FlexTable {

	protected static final int HEADER_ROW = 0;
	protected static final int FIRST_DATA_ROW = 1;
	protected static final int FIRST_COLUMN = 0;
	protected static final int DATA_INDEX_ZERO = 0;
	protected static final int DEFAULT_TABLE_CELL_SPACING = 0;

	private int nextRow = 1;
	private Object[][] rowData = null;

	public BaseFlexTable(Object[][] rowData) {
		this();
		this.rowData = rowData;
		createRows(DATA_INDEX_ZERO);
		applyDataRowStyles();
	}

	protected BaseFlexTable() {
		insertRow(HEADER_ROW);
		getRowFormatter().addStyleName(HEADER_ROW,
				"gwtsolutions-EasyFlexTable-Header");
		setCellSpacing(DEFAULT_TABLE_CELL_SPACING);
		addStyleName("gwtsolutions-EasyFlexTable");
	}

	public void createRows(int rowIndex) {
		if (rowData == null)
			return;

		for (int row = rowIndex; row < rowData.length; row++) {
			addRow(rowData[row]);
		}
	}

	private void addRow(Object[] cellObjects) {
		for (int cell = FIRST_COLUMN; cell < cellObjects.length; cell++) {
			addCell(nextRow, cell, cellObjects[cell]);
		}
		nextRow++;
	}

	public void addCell(int row, int cell, Object cellObject) {
		Widget widget = createCellWidget(cellObject);
		setWidget(row, cell, widget);
		getCellFormatter().addStyleName(
				row,
				cell,
				cell == 0 ? "gwtsolutions-EasyFlexTable-Cell0"
						: "gwtsolutions-EasyFlexTable-Cell");
	}

	public void applyDataRowStyles() {
		HTMLTable.RowFormatter rf = getRowFormatter();
		for (int row = FIRST_DATA_ROW; row < getRowCount(); ++row) {
			if ((row % 2) != 0) {
				// Remove the even row style, just in case this used
				// to be an even row
				rf.removeStyleName(row, "gwtsolutions-EasyFlexTable-EvenRow");
				rf.addStyleName(row, "gwtsolutions-EasyFlexTable-OddRow");
			} else {
				// Remove the odd row style, just in case this used
				// to be an odd row
				rf.removeStyleName(row, "gwtsolutions-EasyFlexTable-OddRow");
				rf.addStyleName(row, "gwtsolutions-EasyFlexTable-EvenRow");
			}
		}
	}

	public void addColumn(Object columnHeading) {
		Widget widget = createCellWidget(columnHeading);
		int columnIndex = getColumnCount();

		widget.setWidth("100%");
		widget.addStyleName("gwtsolutions-EasyFlexTable-ColumnLabel");

		setWidget(HEADER_ROW, columnIndex, widget);

		getCellFormatter().addStyleName(HEADER_ROW, columnIndex,
				"gwtsolutions-EasyFlexTable-ColumnLabelCell");
	}

	public int getColumnCount() {
		return getCellCount(HEADER_ROW);
	}

	protected Widget createCellWidget(Object cellObject) {
		Widget widget = null;

		if (cellObject instanceof Widget)
			widget = (Widget) cellObject;
		else
			widget = new Label(cellObject.toString());

		return widget;

	}
}
